package org.wattdepot.cli.command;

import java.util.List;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * List Summary Class.
 * 
 * @author Remy Baumgarten
 * @author Kevin Chiogioji
 * 
 */
public class ListSummary extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * List Source - Returns a table summarizing the public sources in the system, one per line.
   * Command 2.4.
   * 
   * @param args -
   * @return summary
   */
  public String doCommand(List<String> args) {
    StringBuffer buff = new StringBuffer(255);
    try {
      String location = args.get(0);
      Source query = client.getSource(location);

      String last = location.substring(location.length() - 1, location.length());
      buff.append("Parent: ");
      if (last.matches("\\d")) {
        buff.append(location.substring(0, location.lastIndexOf('_'))).append(cr);
      }
      else if (location.equalsIgnoreCase("SIM_OAHU_GRID")) {
        buff.append("none").append(cr);
      }
      else {
        buff.append("SIM_OAHU_GRID").append(cr);
      }
      buff.append("Subsources: ");
      if (query.isSetSubSources()) {
        List<String> subsource = query.getSubSources().getHref();
        for (String url : subsource) {
          url = url.substring(url.lastIndexOf('/') + 1, url.length());
          buff.append(url).append(", ");
        }
        buff.append(cr);
      }
      else {
        buff.append(cr);
      }
      buff.append("Description: ").append(query.getDescription()).append(cr);
      buff.append("Owner: ").append(query.getOwner()).append(cr);
      buff.append("Location: ").append(query.getLocation()).append(cr);
      buff.append("Coordinates: ").append(query.getCoordinates()).append(cr);
      buff.append("Properties: ");
      if (query.isSetProperties()) {
        for (org.wattdepot.resource.property.jaxb.Property prop : query.getProperties()
            .getProperty()) {
          buff.append('(').append(prop.getKey()).append(" : ").append(prop.getValue()).append(") ");
        }
        buff.append(cr);
      }
      else {
        buff.append("none").append(cr);
      }
      SourceSummary info = null;

      info = client.getSourceSummary(query.getName());

      buff.append("Earliest data: ").append(info.getFirstSensorData()).append(cr);
      buff.append("Latest data: ").append(info.getLastSensorData()).append(cr);
      buff.append("Total data points: ").append(info.getTotalSensorDatas()).append(cr);
    }

    catch (MiscClientException mce) {
      return errorClient;
    }
    catch (BadXmlException bxe) {
      return errorXML;
    }
    catch (ResourceNotFoundException rnfe) {
      return error404;
    }
    catch (NotAuthorizedException nae) {
      return error401;
    }
    catch (IndexOutOfBoundsException iobe) {
      return "bad command";
    }
    return buff.toString();
  }
}
